package com.oxygenecore.util;

import java.io.PrintStream;

/**
 * @author Askar Kalykov
 *         Date: 26.09.2010
 *         Time: 5:44:07
 */
public class ShiftedOutput{
    private String identity;
    private PrintStream out;

    private StringBuilder sb = new StringBuilder();

    public ShiftedOutput(String identity, PrintStream out) {
        this.identity = identity;
        this.out = out;
    }

    public void shift(){
        sb.append(identity);
    }

    public void unshift(){
        if (sb.length() < identity.length()){
            throw new IllegalStateException("can't unshift on top-level");
        }
        sb.setLength(sb.length()- identity.length());
    }

    boolean wantShift = true;

    public void print(String s){
        if (wantShift){
            out.print(sb);
        }
        wantShift = false;
        out.print(s);
    }

    public void println(){
        wantShift = true;
        out.println();
    }

    public void println(String s){
        print(s);
        println();
    }
}
